package com.example.myapplication.fragment;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.example.myapplication.BookBean;
import com.example.myapplication.DBOpenHelper;
import com.example.myapplication.ItemListAdapter;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class f1 extends Fragment implements ItemListAdapter.caa {
    View view;
    Context context;
    private TextView f1text;
    private ImageView imageClearBtn;
    private ImageView imageSearchBtn;
    private EditText footActivityEdit;
    private ListView listview;
    private Button btnInset;
    DBOpenHelper helper;
    SQLiteDatabase db;
    BookBean bookBean;
    List<BookBean>list;
    ItemListAdapter adapter;
    public f1() {
        // Required empty public constructor
    }

    /*
    静态工厂方法
     */
    public static f1 getInstance(String title) {
        f1 f1 = new f1();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        f1.setArguments(bundle);
        return f1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_f1, container, false);
        context = getContext();
        initView();
        final Bundle bundle = getArguments();
        f1text.setText(bundle.getString("title"));
        initData();
        insert();
        return view;
    }
    /*
    新增数据
     */
public void insert(){
    btnInset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
             final EditText diaName;
             final EditText diaAuthor;
             final EditText diaNumber;
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            View view1 = View.inflate(context, R.layout.btn_dialog, null);
            builder.setView(view1);
            diaName = (EditText) view1.findViewById(R.id.dia_name);
            diaAuthor = (EditText) view1.findViewById(R.id.dia_author);
            diaNumber = (EditText) view1.findViewById(R.id.dia_number);

            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                String a1=diaName.getText().toString();
                    String a2=diaAuthor.getText().toString();
                    String a3=diaNumber.getText().toString();
                    bookBean=new BookBean(a1,a2,Integer.valueOf(a3));
                    list.add(0,bookBean);
                    adapter.notifyDataSetChanged();
                    ContentValues values=new ContentValues();
                    values.put("name",a1);
                    values.put("author",a2);
                    values.put("number",Integer.valueOf(a3));
                    db.insert("book",null,values);
                    values.clear();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            final AlertDialog show= builder.show();

        }
    });
}
/*
初始化数据
 */
public void  initData(){
    list=new ArrayList<>();
    helper=new DBOpenHelper(context);
    db=helper.getWritableDatabase();
    Cursor cursor=db.query("book",null,null,null,null,null,null);
    while (cursor.moveToNext()){
        String name=cursor.getString(cursor.getColumnIndex("name"));
        String author=cursor.getString(cursor.getColumnIndex("author"));
        int number=cursor.getInt(cursor.getColumnIndex("number"));
        bookBean=new BookBean(name,author,number);
        list.add(bookBean);
    }
    adapter=new ItemListAdapter(context,list,this);
    listview.setAdapter(adapter);
}
    /*
    初始化控件
     */
    private void initView() {
        f1text = (TextView) view.findViewById(R.id.f1text);
        imageClearBtn = (ImageView) view.findViewById(R.id.image_clear_btn);
        imageSearchBtn = (ImageView) view.findViewById(R.id.image_search_btn);
        footActivityEdit = (EditText) view.findViewById(R.id.foot_activity_edit);
        listview = (ListView) view.findViewById(R.id.listview);
        btnInset = (Button)view. findViewById(R.id.btn_inset);
    }


    @Override
    public void change(int p) {
        list.remove(p);
        adapter.notifyDataSetChanged();
        db.delete("book","name=?",new String[]{list.get(p).getName()});
    }
}
